<!-- 面包屑导航 -->
<template>
  <div class="tags">
    <el-tag
      v-for="(tag, index) in tags"
      :key="tag.name"
      size="small"
      :closable="tag.name !== 'home'"
      :effect="$route.name === tag.name ? 'dark' : 'plain'"
      @click="changeMenu(tag)"
      @close="handleClose(tag, index)"
    >
      {{tag.label}}
    </el-tag>
  </div>
</template>

<script>
import { mapState, mapMutations } from 'vuex'
export default {
  data () {
    return {

    }
  },
  computed: {
    ...mapState({
      tags: (state) => state.tab.tabList
    })
  },
  methods: {
    ...mapMutations({
      close: "closeTag"
    }),
    changeMenu (tag) {
      this.$router.push({ name: tag.name })
      this.$store.commit('SELECTMENU', tag)
    },
    handleClose (tag, index) {
      const length = this.tags.length - 1
      this.close(tag)
      if (tag.name !== this.$route.name) {
        return
      }
      if (index === length) {
        // 如果当前是最靠后的tag，往左边跳
        this.$router.push({ name: this.tags[index - 1].name })
        this.$store.commit("SELECTMENU", this.tags[index - 1])
      } else {
        // 当前tag不是最靠后的，往右边跳
        this.$router.push({ name: this.tags[index].name })
        this.$store.commit("SELECTMENU", this.tags[index])
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.tags {
  text-align: left;
  padding: 20px;
  .el-tag {
    margin-right: 15px;
    cursor: pointer;
  }
}

</style>
